# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
import pymysql.cursors

from twisted.enterprise import adbapi


class ScrapyGifPipeline(object):
    def process_item(self, item, spider):
        return item


class MysqlTwistedGifListItemPipeline(object):
    """
    异步写mysql
    """

    def __init__(self, dbpool):
        self.record = set()
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host=settings['MYSQL_HOST'],
            db=settings['MYSQL_DBNAME'],
            user=settings['MYSQL_USER'],
            password=settings['MYSQL_PASSWORD'],
            port=settings['MYSQL_PORT'],
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor,
            use_unicode=True,
        )
        dbpool = adbapi.ConnectionPool('pymysql', **dbparms)
        return cls(dbpool)

    def process_item(self, item, spider):
        if spider.name == 'gif_spider_list':
            article_id = item.get('article_id', 0)
            if article_id in self.record:
                return item
            else:
                self.record.add(article_id)
                query = self.dbpool.runInteraction(self.do_insert, item)
                query.addErrback(self.handle_error, item, spider)
                return item
        else:
            return item

    def handle_error(self, failure, item, spider):
        article_id = item.get('article_id', 0)
        self.record.remove(article_id)
        print(failure)

    def do_insert(self, cursor, item):
        insert_sql = """
        insert into t_article_list(
        article_id,
        type_name,
        article_img,
        article_title,
        article_detailsSrc,
        article_imgCount,
        article_sendTime,
        article_author,
        article_readCount,
        article_commentsCount,
        article_likeCount,
        article_note,
        article_tag,
        createTime
        ) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
        """
        args = (
            item.get('article_id', 0),
            item.get('type_name', ''),
            item.get('article_img', ''),
            item.get('article_title', ''),
            item.get('article_detailsSrc', ''),
            item.get('article_imgCount', 0),
            item.get('article_sendTime', 0),
            item.get('article_author', ''),
            item.get('article_readCount', 0),
            item.get('article_commentsCount', 0),
            item.get('article_likeCount', 0),
            item.get('article_note', ''),
            item.get('article_tag', ''),
            item.get('createTime', 0),
        )
        cursor.execute(insert_sql, args)


class MysqlTwistedGifDetailsPipeline(object):
    """
    异步写mysql
    """

    def __init__(self, dbpool):
        self.record = set()
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host=settings['MYSQL_HOST'],
            db=settings['MYSQL_DBNAME'],
            user=settings['MYSQL_USER'],
            password=settings['MYSQL_PASSWORD'],
            port=settings['MYSQL_PORT'],
            charset='utf8',
            cursorclass=pymysql.cursors.DictCursor,
            use_unicode=True,
        )
        dbpool = adbapi.ConnectionPool('pymysql', **dbparms)
        return cls(dbpool)

    def process_item(self, item, spider):
        if spider.name == 'gif_spider_details':
            # article_id = item.get('article_id', 0)
            # if article_id in self.record:
            #     return item
            # else:
            #     self.record.add(article_id)
            query = self.dbpool.runInteraction(self.do_insert, item)
            query.addErrback(self.handle_error, item, spider)
            return item
        else:
            return item

    def handle_error(self, failure, item, spider):
        # article_id = item.get('article_id', 0)
        # self.record.remove(article_id)
        print(failure)

    def do_insert(self, cursor, item):
        insert_sql = """
        insert into t_article_details(
        article_id,
        article_img,
        article_text,
        createTime
        ) values (%s, %s, %s, %s)
        """
        args = (
            item.get('article_id', 0),
            item.get('article_img', ''),
            item.get('article_text', ''),
            item.get('createTime', 0),
        )
        cursor.execute(insert_sql, args)
